$(function(){
	//回填
	if(sex==0){
		$("input[name=sex][value='0']").attr("checked",true);
	}else{
		$("input[name=sex][value='1']").attr("checked",true);
	}
	if(photo.length>0){
		$("#LAY_demo_upload").attr("src",ctp +'/'+photo);
	}else{
		$("#LAY_demo_upload").attr("src",'./../images/user-header.jpg');
	}
	$("#edudegree").val(edudegree);
	$("#worktype").val(worktype);
	$("#chargetype").val(chargetype);
	
	
	$("input[name='payform']:radio").each(function(){
		if ($(this).val()==payform) { 
			$(this).attr("checked",true);
		} 
	});
	
	
	//tab切换
	tabStyleTurn('.smart-tab-item');
	tabSwitch('.smart-tab-item');
	$('div[id^=div_]').hide();
	$('#div_salary').show();
	
	//合同回填
	getContractList(recId);
	selectGroups();
	
});

function selectGroups() {
	$
			.ajax({
				url : ctp + "/groupController/selectGroups.shtml",
				data : {},
				type : "POST",
				cache : false,
				dataType : "json",
				success : function(data) {
					var list = data.list;
					var $str = '';
					$str += '<select class="smart-body_select" name="groupid" id="groupid">';
					$str += '<option value="">--请选择--</option>';
					for (var i = 0; i < list.length; i++) {
						var l = list[i];
						$str += '<option value="' + l.REC_ID + '">' + l.NAME
								+ '</option>';
					}
					$str += '</select>';
					$('#group').append($str);
					$('#groupid').val(groupId);
					
				}
			});
}

function uploadFile(){
	//alert(document.getElementById("contract").files[0].name);
	//alert(document.getElementById("contract").files[1].name);
	//alert(document.getElementById("contract").files[2].name);
	var files=document.getElementById("contract").files;
	var fileStr="";
	for(var i=0;i<files.length;i++){
		fileStr+=files[i].name+'|';
	}
	$('#file-name').html(fileStr);
}


//合同回填
function getContractList(recId){
	$.ajax({ 
	 	url: ctp + "/workerController/getWorkerContracts.shtml",
	    data: {recId:recId},
		type : 'POST',
		async:false, 
		dataType: "json",
		success : function(result) {
			var list=result.list;
			var html="";
			for (var i = 0; i < list.length; i++) {
				/*html+='<div id="'+list[i].rec_id+'"><a>'+list[i].name+'</a><a onclick="downContract(\''+list[i].path+'\');">-下载</a><br></div>';*/
				html += '<div class="smart-layer_carousel" id="' + list[i].rec_id + '">'
				+ '<img src="' + ctp + '/' + list[i].path + '" layui-src="' + ctp + '/' + list[i].path + '">'
				+ '<p><a onclick="downContract(\''
				+ list[i].path + '\',\'' + list[i].name
				+ '\');">下载</a>'
				+'</div>';
		    };
		    $('#contractList').html(html);
		    $('#contractList').html(html);
			layui.layer.photos({
		        photos: '#contractList',
		        anim: 0
		    });
		}
	});
}

function delContractByid(recId){
	$.ajax({ 
	 	url: ctp + "/workerController/workerContractDel.shtml",
	    data: {recId:recId},
		type : 'POST',
		async:false, 
		//dataType: "json",
		success : function(result) {
			if(result==''){
				$('#'+recId).remove();
				jalert9("删除成功！");
			}else{
				jalert(msg);
			}
		}
	});
}



function tabSwitch(tabclass) {
	var recId=$('#recId').val();
	$(tabclass).click(function(e) {
		if ("tab_salary" == $(this).attr("id")) {
			$('div[id^=div_]').hide();
			$("#div_salary").show();
		} else if ("tab_jcqk" == $(this).attr("id")) {
			$('div[id^=div_]').hide();
			$("#div_jcqk").show();
			getJcqk(recId);
		} else if ("tab_project" == $(this).attr("id")) {
			$('div[id^=div_]').hide();
			$("#div_project").show();
			getProject(recId);
		}

	});
}


function photoUpload() {
	var photo=$('#photo').val();
	if(photo==undefined||photo.length==0){
		jalert("请选择相片！");
	}
	//附件上传插件渲染
	$('#form1').submit(function() {
		var options = {
			url : ctp + "/workerController/photoUpload.shtml",
			type : 'POST',
			success : function(data) {
				$('#LAY_demo_upload').attr("src", ctp + "/" + data);// 回显图片
				$('#photoPath').val(data);// 设置路径
			}
		};
		$('#form1').ajaxSubmit(options);
		return false;
	});
	$('#form1').submit();
}



function workerAdd(){
	if (!Validator.Validate(document.getElementById("form1"), 1)) {
		return;
	}
	//表单渲染
	$('#form1').submit(function() {
		var options = {
			url : ctp + "/workerController/workerSave.shtml",
			type : 'POST',
			success : function(result) {
				if (result == '1') {
					jalert9("保存成功！");
					setTimeout(function() {
						parent.searchData();
						closeW();
					}, WARN_TIME_OUT);
				} else if (result == '0') {
					jalert("保存失败，请联系管理员！");
				} else if (result == '2') {
					jalert("工号已存在，请更换！");
				}
			}
		};
		$('#form1').ajaxSubmit(options);
		return false;
	});

	$('#form1').submit();

	/*$.ajax({
		url : ctp + "/workerController/workerSave.shtml",
		data : $('#form1').serialize(),
		type : 'POST',
		async : false,
		success : function(result) {
			if (result == '1') {
				jalert9("保存成功！");
				setTimeout(function() {
					parent.searchData();
					back();
				}, WARN_TIME_OUT);
			} else if (result == '0') {
				jalert("保存失败，请联系管理员！");
			} else if (result == '2') {
				jalert("工号已存在，请更换！");
			}
		}
	});*/
	
}





/************************************获取当前用户工程列表开始*********************************************************************/

function getProject(recId) {
	//---开始的页数
	//setTagAndValue("form1","curr",curr); 
	//---每页几条数据
	setTagAndValue("form1","SUNNADA_PAGE_SIZE",6); 
	getProjectInfo(recId);
}

function getProjectInfo(recId){
	$.ajax({
		url: ctp + "/workerController/workerProjectSearch.shtml", 
		data: {recId:recId},
		type: "POST", 
		cache : false, 
		dataType: "json", 
		error: function(request) {
			closeLoading();
		    alert("表单提交出错，请稍候再试");
		},
		success: function(data) {
		    var dataObj = data.list; 
		    var pageJson = data.params;
		    $("#pNum").text(pageJson.rc);
		    //------数据拼接-------
		     projectData(dataObj);
		    //------------   分页开始  -------------
		     //getPaging(pageJson,dataObj);
		    //------------  分页结束      -------------
	    }

	});
}

function projectData(list){
	$("#tbodyData").html("");
	for (var i = 0; i < list.length; i++) {
		var l = list[i];
		$str="";
		$str+="<tr><td>"+l.NAME+"</td>";
		$str+="<td>"+l.GROUPNAME+"</td>";
		$str+="<td>"+l.WORKTYPE+"</td>";
		$str+= "<td>"+l.BGTIME+"</td>";
		$str+= "<td>"+l.EDTIME+"</td>";
		$str+= "<td>"+l.PROJECTSTATE+"</td></tr>";
		$("#tbodyData").append($str);
	}
	
}
/************************************获取当前用户工程列表结束*********************************************************************/



/************************************获取当前用户奖惩情况列表开始*********************************************************************/

function getJcqk(recId) {
	// ---开始的页数
	// setTagAndValue("form1","curr",curr);
	// ---每页几条数据
	setTagAndValue("form1", "SUNNADA_PAGE_SIZE", 6);
	getJcqkInfo(recId);
}

function getJcqkInfo(recId) {
	$.ajax({
		url : ctp + "/workerController/workerJcqkSearch.shtml",
		data : {
			recId : recId
		},
		type : "POST",
		cache : false,
		dataType : "json",
		error : function(request) {
			closeLoading();
			alert("表单提交出错，请稍候再试");
		},
		success : function(data) {
			var dataObj = data.list;
			//var pageJson = data.params;
			//$("#pNum").text(pageJson.rc);
			// ------数据拼接-------
			jcqkData(dataObj);
			// ------------ 分页开始 -------------
			// getPaging(pageJson,dataObj);
			// ------------ 分页结束 -------------
		}

	});
}

function jcqkData(list) {
	$("#tbodyJcqkData").html("");
	for (var i = 0; i < list.length; i++) {
		var l = list[i];
		$str = "";
		$str += "<tr><td>" + l.APPLY_TIME + "</td>";
		if(l.PROJECT==undefined){
			$str += "<td></td>";
		}else{
			$str += "<td>" + l.PROJECT + "</td>";
		}
		$str += "<td>" + l.REWARD_TYPE + "</td>";
		$str += "<td>" + l.REWARD_LEVEL + "</td>";
		$str += "<td>" + l.REWARD_DESC + "</td>";
		$str += "<td>" + l.REWARD_RESULT + "</td>";
		$str += "<td>" + l.AUDIT_RESULT + "</td>";
		$str += "</tr>";
		$("#tbodyJcqkData").append($str);
	}

}
/************************************获取当前用户奖惩情况列表结束*********************************************************************/


//当前弹窗索引
var _index = parent.layui.layer.getFrameIndex(window.name);
function closeW(){
	//parent.layui.layer.close(_index);
	location.href=ctp + "/workerController/worker.shtml";
}